﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using System.ServiceModel;

namespace iLangl.Security
{

	[ServiceBehavior(UseSynchronizationContext = false)]
	public class AuthenticationService : IAuthenticationService
	{


		public string CreateSession(string login, string password)
		{
			MembershipUser muser = Membership.GetUser(login);
			if (muser == null)
			{
				return String.Empty;
			}


			return DAL.CreateSession((Guid)muser.ProviderUserKey);

		}

		public String RegisterUser(string userName, string password)
		{
			MembershipCreateStatus status;
			MembershipUser mu = Membership.CreateUser(userName, password, null, null, null, true, out status);
			return status.ToString();
		}

		public bool IsAuthenticated(string sessionKey)
		{
			return (DAL.ReadSession(sessionKey) != null);
		}

		public string GetUserName(string sessionKey)
		{
			Session session =  DAL.ReadSession(sessionKey);

			if(session == null)
				return string.Empty;

			return session.UserName;
			
		}

		public string GetUserId(string sessionKey)
		{
			Session session = DAL.ReadSession(sessionKey);

			if (session == null)
				return string.Empty;

			return session.UserId.ToString();
		}

		private AuthenticationServiceDAL DAL
		{
			get
			{
				return new AuthenticationServiceDAL();
			}
		}


	
	}
}